<?php
namespace Crm\Api;

use PhalApi\Api;
use PhalApi\Request;
use PhalApi\Exception\BadRequestException;
use Auth\Api\Token as Token;
use Crm\Domain\Customer as DomainCustomer;


/**
 * 客户接口服务
 *
 * @author: luodong <751450467@qq.com> 2020-01-04
 */

class Customer extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

    public function getRules() {
        return array(
            //客户接口
            'getParams'=> array(
                'require_type' => array('name' => 'require_type', 'type'=>'string', 'require' => true, 'desc' => '请求参数类型')
            ),
            'getList' => array(),
            'add' => array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'mobile', 'require' => true, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
            ),
            'update' => array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID'),
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'crm_mobile', 'require' => false, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
            ),
            'detail' => array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID')
            ),
            'baseInfo' => array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID')
            ),

            //客户跟进
            'listLog' =>array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID')
            ),

            //到访接口
            'listVisit' =>array(
                'customer_id' => array('name' => 'customer_id', 'type'=>'int', 'require' => true, 'desc' => '客户ID')
            ),
            "addVisit"=> array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'visit_type'  => array('name' => 'visit_type', 'require' => true, 'desc'=>'类型'),
                'type'  => array('name' => 'type', 'require' => true, 'desc'=>'数据类型类型(fc_esf,fc_project)'),
                'customer_id'  => array('name' => 'customer_id', 'require' => true, 'desc'=>'客户id'),
                'action_id'  => array('name' => 'action_id', 'require' => true, 'desc'=>'项目id')
            ),
            'addCustomerAndVisit'=> array(
                'name' 	=> array('name' => 'name', 'type'=>'string', 'require' => true, 'min' => 1, 'max' => '30', 'desc' => '客户姓名'),
                'mobile'  => array('name' => 'mobile', 'require' => true, 'regex' => "/^(13[0-9]|14[5|7|9]|15[0|1|2|3|5|6|7|8|9]|16[6]|17[0|1|2|3|5|6|7|8]|18[0-9]|19[8|9])\d{8}$/i", 'desc'=>'客户手机'),
                'visit_type'  => array('name' => 'visit_type', 'require' => true, 'desc'=>'类型'),
                'type'  => array('name' => 'type', 'require' => true, 'desc'=>'数据类型类型(fc_esf,fc_project)'),
            ),
            'detailVisit' => array(
                'visit_id' => array('name' => 'visit_id', 'type'=>'int', 'require' => true, 'desc' => '日志id'),
            ),
        );

    }

    /**
     * 获取搜索参数
     * @desc 获取搜索栏参数定义
     * @return obj 参数信息
     */
    public function getParams(){
        $domain = new DomainCustomer();
        $params['warn_outdays'] = $domain->baseCode->customer['warn_outdays'];
        $params['danger_outdays'] = $domain->baseCode->customer['danger_outdays'];

        $params['origin_option'] = $domain->metaData->toKeyValue($domain->origin_option);
        $params['statusData'] = $domain->metaData->toKeyValue($domain->baseCode->customer_status_option);

        $params['mainData'] = ["状态", "价格", "面积", "更多", "排序"];
        $params['rangData'] = [['value'=>'全部','key'=> 'all'],['value'=>'我的','key'=>'my'],['value'=>'我部门的','key'=>'department']];
        $params['priceData'] = [
            [
                'value'=> '单价',
                'key'=>'1010201',
                'data'=> [
                    ['value'=>'不限', 'key'=> '0'],
                    ['value'=>'7千以下', 'key'=> '0-7000'],
                    ['value'=>'1-1.5万', 'key'=> '10000-15000'],
                    ['value'=>'1-2万', 'key'=> '10000-20000'],
                    ['value'=>'2-3万', 'key'=> '20000-30000'],
                    ['value'=>'3-5万', 'key'=> '30000-50000'],
                    ['value'=>'5万元以上', 'key'=> '50000-0']
                ]
            ],
            [
                'value'=>'总价',
                'key'=>'1010202',
                'data'=>[
                    ['value'=>'不限', 'key'=> '0'],
                    ['value'=>'50万以内', 'key'=> '0-50'],
                    ['value'=>'50-80万', 'key'=> '50-80'],
                    ['value'=>'80-100万', 'key'=> '80-100'],
                    ['value'=>'100-150万', 'key'=> '100-150'],
                    ['value'=>'150-200万', 'key'=> '150-200'],
                    ['value'=>'200-300万', 'key'=> '200-300'],
                    ['value'=>'300-500万', 'key'=> '300-500'],
                    ['value'=>'500-800万', 'key'=> '500-800'],
                    ['value'=>'800-1200万', 'key'=> '800-1200'],
                    ['value'=>'1200万以上', 'key'=> '1200-0']
                ]
            ]
        ];

        $params['huxingData'] = [['value'=>'一室', 'key'=>'1' ], ['value'=>'二室', 'key'=>'2' ], ['value'=>'三室', 'key'=>'3' ], ['value'=>'四室', 'key'=>'4' ], ['value'=>'五室', 'key'=>'5' ], ['value'=>'五室以上', 'key'=>'6' ]];

        $params['boroughData'] = [['value'=>'全部', 'key'=>'0' ], ['value'=>'锦江区', 'key'=>'510104' ], ['value'=>'青羊区', 'key'=>'510105' ], ['value'=>'金牛区', 'key'=>'510106' ], ['value'=>'武侯区', 'key'=>'510107' ], ['value'=>'成华区', 'key'=>'510108' ],['value'=>'高新区', 'key'=>'510109' ]];

        $params['areaData'] = [['value'=>'全部', 'key'=>'0'], ['value'=>'100m²以下', 'key'=>'0-100'], ['value'=>'100-150m²', 'key'=>'100-150'], ['value'=>'150-200m²', 'key'=>'150-200'], ['value'=>'200-300m²', 'key'=>'200-300'], ['value'=>'300-500m²', 'key'=>'300-500'], ['value'=>'500-1000m²', 'key'=>'500-1000'], ['value'=>'1000m²以上', 'key'=>'1000-0']];

        $params['floorData']= [['value'=>'不限', 'key'=>'0'], ['value'=>'1-6层', 'key'=>'1-6'],['value'=>'7-12层', 'key'=>'7-12'],
            ['value'=>'15-20层', 'key'=>'15-20'],['value'=>'20-30层', 'key'=>'20-30'],['value'=>'30层以上', 'key'=>'30-0']];
        $params['fitmentData']= [['value'=>'清水', 'key'=>'1'], ['value'=>'精装', 'key'=>'3']];
        $params['orderData']= [['value'=>'默认排序', 'key'=>'a.create_date desc,a.rating asc'], ['value'=>'跟进时间排序', 'key'=>'a.log_date desc'], ['value'=>'等级排序', 'key'=>'a.rating asc']];

        return $params;
    }

    /**
     * 返回客户列表
     * @desc 添加意向客户信息(客户姓名、性别、手机、资金需求量、客户类型(资质)、客户来源、地区)
     * @return obj 客户信息
     */
    public function getList() {
        $this->user = $this->token->checkToken();

        $domain = new DomainCustomer();
        $rs = $domain->search($this->user);
        return $rs;
    }


    /**
     * 新增客户
     * @desc 添加意向客户信息(客户姓名、性别、手机、资金需求量、客户类型(资质)、客户来源、地区)<br>Authorization Type: Bear Token<br>"headers" : {'Authorization': 'Bearer '+ token}
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function add() {
        $this->user = $this->token->checkToken();
        error_log("addCustomer!", 0);

        $params =  \PhalApi\DI()->request->getAll();
        $domain = new DomainCustomer();
        $id = $domain->add($params,$this->user);
        return $id;
    }

    /**
     * 更新意向用户
     * @desc 添加意向客户信息(客户姓名、性别、手机、资金需求量、客户类型(资质)、客户来源、地区等)
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */
    public function update(){
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);

        $params =  \PhalApi\DI()->request->getAll();
        $domain = new DomainCustomer();
        $id = $domain->update($params,$this->user);

        return $id;
    }

    /**
     * 客户详情
     * @desc 获取客户详情
     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detail()
    {
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);

        $domain = new DomainCustomer();
        $data = $domain->getCustomerInfoById($this->customer_id);

        return $data;
    }

    /**
     * 客户基础信息
     * @desc 客户基础信息
     * @return array data 客户基础信息
     * @return int time 当前时间戳
     */
    public function baseInfo()
    {
        $this->user = $this->token->checkToken();
        error_log("updateCustomer!", 0);

        $domain = new DomainCustomer();
        $data = $domain->getCustomerInfoById($this->customer_id,'baseInfo');

        return $data;
    }

    /**
     * 客户跟进
     * @desc 获取该客户跟进
     * @return array data 客户跟进信息
     * @return int time 当前时间戳
     */
    public function listLog(){
        $this->user = $this->token->checkToken();
        error_log("listLog!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->getCustomerLog();

        return $data;
    }

    /**
     * 客户到访
     * @desc 获取该客户带看
     * @return array data 客户带看信息
     * @return int time 当前时间戳
     */
    public function listVisit(){
        $this->user = $this->token->checkToken();
        error_log("listVisit!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->getCustomerVisit();

        return $data;
    }

    /**
     * 新增根据记录
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function addVisit() {
        $this->user = $this->token->checkToken();
        error_log("AddVisit!", 0);
        //插入主表数据
        $domain = new DomainCustomer();
        $id = $domain->addVisit($this->user["user_id"]);
        return $id;
    }

    /**
     * 新增客户并添加到访
     * @desc 添加客户同时新增到访
     * @return int id 新增的ID
     * @return int time 当前时间戳
     */

    public function addCustomerAndVisit() {
        $this->user = $this->token->checkToken();

        error_log("addCustomerAndVisit!", 0);
        //新增客户并添加到访数据
        $domain = new DomainCustomer();
        $id = $domain->addCustomerAndVisit($this->user["user_id"]);
        return $id;
    }

    /**
     * 返回带看回访列表
     * @return obj 带看回访数据
     */
    public function getVisitList() {
        $this->user = $this->token->checkToken();

        $domain = new DomainCustomer();
        $rs = $domain->getVisitList($this->user["user_id"]);
        return $rs;
    }

    /**
     * 获取一条日志详情

     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detailVisit()
    {
        $this->user = $this->token->checkToken();
        error_log("DetailVisit!", 0);
        //主表数据
        $params =  \PhalApi\DI()->request->getAll();
        //插入主表数据
        $domain = new DomainCustomer();
        $data = $domain->detailVisit($params["visit_id"],$this->user["user_id"]);
        return $data;
    }

}
